package com.sgr.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.sgr.dao.ClienteDao;
import com.sgr.model.Cliente;

	public class ClienteDaoImpl implements ClienteDao {
		
		@Autowired
		private SessionFactory sessionFactory;

		public void setSessiionFactory(SessionFactory sessionFactory){
			this.sessionFactory = sessionFactory;
		}
		
		public Session getCurrentSession() {
			return this.sessionFactory.getCurrentSession();
		}

		public SessionFactory getSessionFactory(){
			return sessionFactory;
		}
		
		@Override
		@Transactional
		public void save(Cliente cli){
			getCurrentSession().save(cli);
		}
				
		@Override
		@Transactional
		public void update(Cliente cli){
			getCurrentSession().update(cli);
		}
		
		@Override
		@Transactional
		public void delete(Cliente cli){
			getCurrentSession().delete(cli);
		}
		
		@Override
		@Transactional
		public List findByName(String name){
			return null;
		}
		
		@Override
		@Transactional
		public Long count(){
			Long rows =(Long) getCurrentSession().createCriteria(Cliente.class).setProjection(Projections.rowCount()).uniqueResult();
			return rows;
		}
		@Override
		@Transactional
		public List findAll(){
			List Cliente = getCurrentSession().createQuery("from Cliente").list();
			return Cliente;
		}
		@Override
		@Transactional
		public Cliente findByPk(int id){
			Cliente cliente = (Cliente)getCurrentSession().createCriteria(Cliente.class).add(Restrictions.eq("id",id)).uniqueResult();
			return cliente;
	}
}

